package com.biz.primus.management.coupon.type.tag;

import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 优惠券类型商品选择标签
 *
 * @author david-liu
 */
@Setter
@Slf4j
public class CouponTypeProductSelectTag extends TagSupport {
    private static final long serialVersionUID = -8617662727002914451L;

    private String fieldId;

    private List<Long> fieldValues;

    @Override
    public int doStartTag() throws JspException {
        JspWriter jspWriter = pageContext.getOut();
        List<String> values = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(fieldValues)) {
            fieldValues.forEach(value -> values.add(String.format("name%d", value)));
        }
        String html = String.format("<input type=\"text\" id=\"%s-names\" value=\"%s\" readonly/>", fieldId, StringUtils.join(values, ","));
        try {
            jspWriter.print(html);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return super.doStartTag();
    }
}
